package com.china.school.listener;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.time.Clock;

public class EventListenerTest implements ApplicationListener<EventListenerTest.TestEvent> {


    @Override
    public void onApplicationEvent(TestEvent event) {
        System.out.println("监听到了事件-----------------" + event);
    }

    public static class TestEvent extends ApplicationEvent {

        public TestEvent(Object source) {
            super(source);
        }

        public TestEvent(Object source, Clock clock) {
            super(source, clock);
        }
    }


    @Component
    public static class ApplicationEventPublisherAwareTest implements ApplicationEventPublisherAware {
        private ApplicationEventPublisher applicationEventPublisher ;

        @Override
        public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
            this.applicationEventPublisher = applicationEventPublisher;

        }
        public ApplicationEventPublisherAwareTest( ) {
        }
        public void publishEvent(){
            System.out.println("发布事件-----------");
            this.applicationEventPublisher.publishEvent(new EventListenerTest.TestEvent("下雪了"));

        }
    }
}
